<template>
  <div class="user-avator-dropdown">
    <Dropdown @on-click="handleClick">
      <Badge>
        <Avatar :src="userAvator"/>
      </Badge>
      <span style="margin-left: 5px">{{userName}}</span>
      <DropdownMenu slot="list">
        <DropdownItem name="user">用户中心</DropdownItem>
        <DropdownItem name="logout">退出登录</DropdownItem>
      </DropdownMenu>
    </Dropdown>
  </div>
</template>

<script>
import './user.less'
import { mapActions } from 'vuex'
import router from '../../../../router'
export default {
  name: 'User',
  props: {
    userName: {
      type: String,
      default: ''
    },
    userAvator: {
      type: String,
      default: ''
    }
  },
  methods: {
    ...mapActions([
      'handleLogOut'
    ]),
    logout () {
      this.handleLogOut().then(() => {
        this.$store.commit('setMenu', [])
        router.options.routes = store.state.app.menu
        this.$router.push({
          name: 'login'
        })
      })
    },
    user () {
      this.$emit('on-user')
    },
    handleClick (name) {
      switch (name) {
        case 'logout': this.logout()
          break
        case 'user': this.user()
          break
      }
    }
  }
}
</script>
